
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Changesets · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="associations.html" />
    
    
    <link rel="prev" href="basics.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="basics.html">
            
                <a href="basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.2" data-path="changesets.html">
            
                <a href="changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="associations.html">
            
                <a href="associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="querying.html">
            
                <a href="querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Changesets</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x4E3A;&#x4E86;&#x5F80;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x63D2;&#x5165;&#xFF0C;&#x66F4;&#x65B0;&#x6216;&#x662F;&#x5220;&#x9664;&#x6570;&#x636E;&#xFF0C;<code>Ecto.Repo.insert/2</code>, <code>update/2</code> &#x548C; <code>delete/2</code> &#x90FD;&#x9700;&#x8981;&#x4E00;&#x4E2A; changeset &#x4F5C;&#x4E3A;&#x5B83;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x4EC0;&#x4E48;&#x662F; changesets &#x5462;?</p>
<p>&#x51E0;&#x4E4E;&#x6BCF;&#x4E2A;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x90FD;&#x719F;&#x6089;&#x7684;&#x4E00;&#x4E2A;&#x4EFB;&#x52A1;&#x662F;&#x68C0;&#x67E5;&#x8F93;&#x5165;&#x6570;&#x636E;&#x662F;&#x5426;&#x5B58;&#x5728;&#x6F5C;&#x5728;&#x7684;&#x9519;&#x8BEF; - &#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x5728;&#x5C1D;&#x8BD5;&#x5C06;&#x6570;&#x636E;&#x7528;&#x4E8E;&#x6211;&#x4EEC;&#x7684;&#x76EE;&#x7684;&#x4E4B;&#x524D;&#x786E;&#x4FDD;&#x6570;&#x636E;&#x662F;&#x5904;&#x4E8E;&#x6B63;&#x786E;&#x7684;&#x72B6;&#x6001;&#x3002;</p>
<p>Ecto &#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x6709;&#x5173;&#x6570;&#x636E;&#x66F4;&#x6539;&#x7684;&#x5B8C;&#x6574;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF0C;Ecto &#x5C06;&#x76F8;&#x5173;&#x7684;&#x51FD;&#x6570;&#x548C;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x96C6;&#x5408;&#x5728; <code>Changeset</code> &#x6A21;&#x5757;&#x4E2D;&#x3002; &#x5728;&#x672C;&#x8282;&#x8BFE;&#x7A0B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5B66;&#x4E60; <code>Changeset</code> &#x6709;&#x5173;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x4EE5;&#x53CA;&#x5982;&#x4F55;&#x5728;&#x5C06;&#x6570;&#x636E;&#x4FDD;&#x5B58;&#x5230;&#x6570;&#x636E;&#x5E93;&#x4E4B;&#x524D;&#x6821;&#x9A8C;&#x6570;&#x636E;&#x7684;&#x5B8C;&#x6574;&#x6027;&#x76F8;&#x5173;&#x7684;&#x77E5;&#x8BC6;&#x3002;</p>
<h2 id="&#x521B;&#x5EFA;&#x4F60;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;-changeset">&#x521B;&#x5EFA;&#x4F60;&#x7684;&#x7B2C;&#x4E00;&#x4E2A; changeset</h2>
<p>&#x9996;&#x5148;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E00;&#x4E2A;&#x7A7A;&#x7684; <code>%Changeset{}</code> &#x7ED3;&#x6784;&#x662F;&#x4EC0;&#x4E48;&#x6837;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; %Ecto.Changeset{}
%Ecto.Changeset&lt;<span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>, <span class="hljs-symbol">changes:</span> %{}, <span class="hljs-symbol">errors:</span> [], <span class="hljs-symbol">data:</span> <span class="hljs-keyword">nil</span>, <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">false</span>&gt;
</code></pre>
<p>&#x6B63;&#x5982;&#x4F60;&#x6240;&#x770B;&#x5230;&#x7684;&#xFF0C;&#x5B83;&#x6709;&#x4E00;&#x4E9B;&#x53EF;&#x80FD;&#x6709;&#x7528;&#x7684;&#x5B57;&#x6BB5;&#xFF0C;&#x4F46;&#x5B83;&#x4EEC;&#x90FD;&#x662F;&#x7A7A;&#x7684;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x4F7F; changeset &#x771F;&#x6B63;&#x6709;&#x7528;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x5B83;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x7684;&#x5927;&#x81F4;&#x7ED3;&#x6784;&#x3002;&#x6709;&#x4EC0;&#x4E48;&#x6BD4;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x7684; schema &#x66F4;&#x51C6;&#x786E;&#x5730;&#x63CF;&#x8FF0;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x7684;&#x5B57;&#x6BB5;&#x53CA;&#x5176;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x5462;&#xFF1F;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x4E00;&#x4E2A;&#x6700;&#x7B80;&#x5355;&#x5E38;&#x89C1;&#x7684; User &#x7684; Schema&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">User</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Schema

  schema <span class="hljs-string">&quot;users&quot;</span> <span class="hljs-keyword">do</span>
    field(<span class="hljs-symbol">:name</span>, <span class="hljs-symbol">:string</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8981;&#x5229;&#x7528; <code>User</code> &#x7684; Schema &#x521B;&#x5EFA; changeset &#x7684;&#x8BDD;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>Ecto.Changeset.cast/4</code></p>
<pre><code class="lang-elixir">iex&gt; Ecto.Changeset.cast(%User{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Bob&quot;</span>}, %{}, [<span class="hljs-symbol">:name</span>])
%Ecto.Changeset&lt;<span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>, <span class="hljs-symbol">changes:</span> %{}, <span class="hljs-symbol">errors:</span> [], <span class="hljs-symbol">data:</span> <span class="hljs-comment">#User&lt;&gt;,</span>
 <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">true</span>&gt;
</code></pre>
<p>&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x539F;&#x59CB;&#x6570;&#x636E; - &#x5728;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E0B;&#x662F;&#x4E3A;&#x7A7A;&#x7684; <code>&#xFF05;User{}</code> &#x7ED3;&#x6784;&#x3002; Ecto &#x975E;&#x5E38;&#x806A;&#x660E;&#xFF0C;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x7ED3;&#x6784;&#x672C;&#x8EAB;&#x627E;&#x5230;&#x5BF9;&#x5E94;&#x7684; Schema&#x3002; &#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x505A;&#x51FA;&#x7684;&#x66F4;&#x65B0; - &#x8FD9;&#x91CC;&#x662F;&#x4E00;&#x4E2A;&#x7A7A;&#x7684; map &#x7ED3;&#x6784;&#x3002; &#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x662F; <code>cast/4</code> &#x7279;&#x522B;&#x7684;&#x539F;&#x56E0;: &#x5B83;&#x5305;&#x542B;&#x4E86;&#x5141;&#x8BB8;&#x901A;&#x8FC7;&#x7684;&#x5B57;&#x6BB5;&#x5217;&#x8868;&#xFF0C;&#x8FD9;&#x4F7F;&#x6211;&#x4EEC;&#x80FD;&#x591F;&#x63A7;&#x5236;&#x54EA;&#x4E9B;&#x5B57;&#x6BB5;&#x53EF;&#x4EE5;&#x66F4;&#x6539;&#xFF0C;&#x5E76;&#x4FDD;&#x62A4;&#x5269;&#x4E0B;&#x7684;&#x5B57;&#x6BB5;&#x3002;</p>
<pre><code class="lang-elixir"> iex&gt; Ecto.Changeset.cast(%User{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Bob&quot;</span>}, %{<span class="hljs-string">&quot;name&quot;</span> =&gt; <span class="hljs-string">&quot;Jack&quot;</span>}, [<span class="hljs-symbol">:name</span>])
 %Ecto.Changeset&lt;
  <span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">changes:</span> %{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Jack&quot;</span>},
  <span class="hljs-symbol">errors:</span> [],
  <span class="hljs-symbol">data:</span> <span class="hljs-comment">#User&lt;&gt;,</span>
  <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">true</span>
&gt;

iex&gt; Ecto.Changeset.cast(%User{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Bob&quot;</span>}, %{<span class="hljs-string">&quot;name&quot;</span> =&gt; <span class="hljs-string">&quot;Jack&quot;</span>}, [])
%Ecto.Changeset&lt;<span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>, <span class="hljs-symbol">changes:</span> %{}, <span class="hljs-symbol">errors:</span> [], <span class="hljs-symbol">data:</span> <span class="hljs-comment">#User&lt;&gt;,</span>
 <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">true</span>&gt;
</code></pre>
<p>&#x60A8;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x7B2C;&#x4E8C;&#x6B21;&#x7684;&#x66F4;&#x65B0;&#x5FFD;&#x7565;&#x4E86;&#x65B0;&#x7684; name &#x503C;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x91CC;&#x6CA1;&#x6709;&#x5141;&#x8BB8; name &#x5B57;&#x6BB5;&#x3002;</p>
<p><code>cast/4</code> &#x51FD;&#x6570;&#x7684;&#x4E00;&#x4E2A;&#x66FF;&#x4EE3;&#x65B9;&#x6848;&#x662F; <code>change/2</code> &#x51FD;&#x6570;&#xFF0C;&#x4F46;&#x5B83;&#x4E0D;&#x80FD;&#x50CF; <code>cast/4</code> &#x90A3;&#x6837;&#x8FC7;&#x6EE4;&#x4E00;&#x4E9B;&#x66F4;&#x6539;&#x3002; &#x5F53;&#x60A8;&#x4FE1;&#x4EFB;&#x8FDB;&#x884C;&#x66F4;&#x6539;&#x7684;&#x6E90;&#x6570;&#x636E;&#x6216;&#x624B;&#x52A8;&#x5904;&#x7406;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x521B;&#x5EFA; changesets&#xFF0C;&#x4F46;&#x7531;&#x4E8E;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x6821;&#x9A8C;&#xFF0C;&#x5BF9; name &#x6240;&#x505A;&#x7684;&#x4EFB;&#x4F55;&#x66F4;&#x6539;&#x90FD;&#x4F1A;&#x88AB; Ecto &#x63A5;&#x53D7;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x6700;&#x7EC8;&#x53EF;&#x80FD;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x503C;&#x4E3A;&#x7A7A;&#x7684; name&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Ecto.Changeset.cast(%User{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Bob&quot;</span>}, %{<span class="hljs-string">&quot;name&quot;</span> =&gt; <span class="hljs-string">&quot;&quot;</span>}, [<span class="hljs-symbol">:name</span>])
%Ecto.Changeset&lt;
 <span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>,
 <span class="hljs-symbol">changes:</span> %{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;&quot;</span>},
 <span class="hljs-symbol">errors:</span> [],
 <span class="hljs-symbol">data:</span> <span class="hljs-comment">#User&lt;&gt;,</span>
 <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">true</span>
&gt;
</code></pre>
<p>Ecto &#x8BA4;&#x4E3A;&#x8FD9;&#x4E2A; changeset &#x662F;&#x5408;&#x6CD5;&#x7684; &#xFF08;<code>valid?: true</code>)&#xFF0C;&#x4F46;&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x5E0C;&#x671B;&#x7528;&#x6237;&#x540D;&#x5B57;&#x4E3A;&#x7A7A;&#x3002; &#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x6765;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x3002;</p>
<h2 id="&#x6821;&#x9A8C;">&#x6821;&#x9A8C;</h2>
<p>Ecto &#x9644;&#x5E26;&#x4E86;&#x8BB8;&#x591A;&#x5185;&#x7F6E;&#x7684;&#x6821;&#x9A8C;&#x51FD;&#x6570;&#x6765;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5927;&#x91CF;&#x4F7F;&#x7528; <code>Ecto.Changeset</code> &#x63D0;&#x4F9B;&#x7684;&#x6821;&#x9A8C;&#x51FD;&#x6570;&#xFF0C;&#x6240;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x5C06; <code>Ecto.Changeset</code> &#x5BFC;&#x5165;&#x5230;&#x5305;&#x542B;&#x4E86; schema &#x7684; <code>user.ex</code> &#x6A21;&#x5757;&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">User</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> Ecto.Schema
  import Ecto.Changeset

  schema <span class="hljs-string">&quot;users&quot;</span> <span class="hljs-keyword">do</span>
    field(<span class="hljs-symbol">:name</span>, <span class="hljs-symbol">:string</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528; <code>cast/4</code> &#x51FD;&#x6570;&#x4E86;&#x3002;</p>
<p>&#x901A;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#x6211;&#x4EEC;&#x4F1A;&#x4E3A; Schema &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A; changeset &#x6784;&#x9020;&#x51FD;&#x6570;&#x3002; &#x8BA9;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x63A5;&#x53D7;&#x7ED3;&#x6784;&#xFF0C;&#x53D8;&#x66F4;&#x6620;&#x5C04;&#x5E76;&#x8FD4;&#x56DE; changeset &#x7684;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">changeset</span></span>(struct, params) <span class="hljs-keyword">do</span>
  struct
  |&gt; cast(params, [<span class="hljs-symbol">:name</span>])
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x786E;&#x4FDD; <code>name</code> &#x59CB;&#x7EC8;&#x5B58;&#x5728;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">changeset</span></span>(struct, params) <span class="hljs-keyword">do</span>
  struct
  |&gt; cast(params, [<span class="hljs-symbol">:name</span>])
  |&gt; validate_required([<span class="hljs-symbol">:name</span>])
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5F53;&#x6211;&#x4EEC;&#x8C03;&#x7528; <code>User.changeset/2</code> &#x51FD;&#x6570;&#x5E76;&#x4F20;&#x9012;&#x4E00;&#x4E2A;&#x503C;&#x4E3A;&#x7A7A;&#x7684; name &#x65F6;&#xFF0C;changeset &#x5C06;&#x4E0D;&#x518D;&#x6709;&#x6548;&#xFF0C;&#x8FD8;&#x4F1A;&#x5305;&#x542B;&#x6709;&#x7528;&#x7684;&#x9519;&#x8BEF;&#x6D88;&#x606F;&#x3002; &#x6CE8;&#x610F;&#xFF1A;&#x5728; <code>iex</code> &#x4E2D;&#x8C03;&#x7528;&#x65F6;&#x4E0D;&#x8981;&#x5FD8;&#x8BB0;&#x8FD0;&#x884C; <code>recompile()</code> &#x547D;&#x4EE4;&#xFF0C;&#x5426;&#x5219;&#x5B83;&#x5C06;&#x65E0;&#x6CD5;&#x83B7;&#x53D6;&#x60A8;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x6240;&#x505A;&#x7684;&#x66F4;&#x6539;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; User.changeset(%User{}, %{<span class="hljs-string">&quot;name&quot;</span> =&gt; <span class="hljs-string">&quot;&quot;</span>})
%Ecto.Changeset&lt;
  <span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">changes:</span> %{},
  <span class="hljs-symbol">errors:</span> [<span class="hljs-symbol">name:</span> {<span class="hljs-string">&quot;can&apos;t be blank&quot;</span>, [<span class="hljs-symbol">validation:</span> <span class="hljs-symbol">:required</span>]}],
  <span class="hljs-symbol">data:</span> <span class="hljs-comment">#User&lt;&gt;,</span>
  <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">false</span>
&gt;
</code></pre>
<p>&#x5982;&#x679C;&#x60A8;&#x5C1D;&#x8BD5;&#x4F7F;&#x7528;&#x4E0A;&#x9762;&#x7684;changeset&#x6267;&#x884C; <code>Repo.insert(changeset)</code>&#xFF0C;&#x60A8;&#x5C06;&#x6536;&#x5230; <code>{:error&#xFF0C;changeset}</code> &#x8FD4;&#x56DE;&#x76F8;&#x540C;&#x7684;&#x9519;&#x8BEF;&#xFF0C;&#x56E0;&#x6B64;&#x60A8;&#x4E0D;&#x5FC5;&#x6BCF;&#x6B21;&#x90FD;&#x81EA;&#x5DF1;&#x68C0;&#x67E5; <code>changeset.valid?</code>&#x3002;&#x8FD9;&#x8BA9;&#x5C1D;&#x8BD5;&#x6267;&#x884C;&#x63D2;&#x5165;&#xFF0C;&#x66F4;&#x65B0;&#x6216;&#x5220;&#x9664;&#xFF0C;&#x4EE5;&#x53CA;&#x5904;&#x7406;&#x9519;&#x8BEF;&#x7684;&#x64CD;&#x4F5C;&#x53D8;&#x5F97;&#x5BB9;&#x6613;&#xFF0C;&#x5047;&#x5982;&#x6709;&#x9519;&#x8BEF;&#x7684;&#x8BDD;&#x3002;</p>
<p>&#x9664;&#x4E86; <code>validate_required/2</code> &#x4E4B;&#x5916;&#xFF0C;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x9700;&#x8981;&#x989D;&#x5916;&#x53C2;&#x6570;&#x7684; <code>validate_length/3</code> &#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">changeset</span></span>(struct, params) <span class="hljs-keyword">do</span>
  struct
  |&gt; cast(params, [<span class="hljs-symbol">:name</span>])
  |&gt; validate_required([<span class="hljs-symbol">:name</span>])
  |&gt; validate_length(<span class="hljs-symbol">:name</span>, <span class="hljs-symbol">min:</span> <span class="hljs-number">2</span>)
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x60A8;&#x53EF;&#x4EE5;&#x731C;&#x4E00;&#x4E0B;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x4F20;&#x5355;&#x4E2A;&#x5B57;&#x7B26;&#x7684; name&#xFF0C;&#x7ED3;&#x679C;&#x4F1A;&#x662F;&#x4EC0;&#x4E48;&#xFF01;</p>
<pre><code class="lang-elixir">iex&gt; User.changeset(%User{}, %{<span class="hljs-string">&quot;name&quot;</span> =&gt; <span class="hljs-string">&quot;A&quot;</span>})
%Ecto.Changeset&lt;
  <span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">changes:</span> %{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;A&quot;</span>},
  <span class="hljs-symbol">errors:</span> [
    <span class="hljs-symbol">name:</span> {<span class="hljs-string">&quot;should be at least %{count} character(s)&quot;</span>,
     [<span class="hljs-symbol">count:</span> <span class="hljs-number">2</span>, <span class="hljs-symbol">validation:</span> <span class="hljs-symbol">:length</span>, <span class="hljs-symbol">min:</span> <span class="hljs-number">2</span>]}
  ],
  <span class="hljs-symbol">data:</span> <span class="hljs-comment">#User&lt;&gt;,</span>
  <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">false</span>
&gt;
</code></pre>
<p>&#x60A8;&#x53EF;&#x80FD;&#x4F1A;&#x5947;&#x602A;&#x4E3A;&#x4EC0;&#x4E48;&#x9519;&#x8BEF;&#x6D88;&#x606F;&#x5305;&#x542B;&#x4E86;&#x795E;&#x79D8;&#x7684; <code>&#xFF05;{count}</code> &#x7B26;&#x53F7; - &#x8FD9;&#x662F;&#x4E3A;&#x4E86;&#x8F85;&#x52A9;&#x7FFB;&#x8BD1;&#x6210;&#x5176;&#x4ED6;&#x8BED;&#x8A00;; &#x5982;&#x679C;&#x4F60;&#x60F3;&#x76F4;&#x63A5;&#x5411;&#x7528;&#x6237;&#x663E;&#x793A;&#x9519;&#x8BEF;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; [<code>traverse_errors/2</code>] (<a href="https://hexdocs.pm/ecto/Ecto.Changeset.html#traverse_errors/2)&#x4F7F;&#x5B83;&#x4EEC;&#x6210;&#x4E3A;&#x7528;&#x6237;&#x53EF;&#x8BFB;&#x7684;&#x4FE1;&#x606F;" target="_blank">https://hexdocs.pm/ecto/Ecto.Changeset.html#traverse_errors/2)&#x4F7F;&#x5B83;&#x4EEC;&#x6210;&#x4E3A;&#x7528;&#x6237;&#x53EF;&#x8BFB;&#x7684;&#x4FE1;&#x606F;</a> - &#x4F60;&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x6587;&#x6863;&#x4E2D;&#x63D0;&#x4F9B;&#x7684;&#x793A;&#x4F8B;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F; <code>Ecto.Changeset</code> &#x4E2D;&#x7684;&#x4E00;&#x4E9B;&#x5176;&#x4ED6;&#x5185;&#x7F6E;&#x7684; validators&#xFF1A;</p>
<ul>
<li>validate_acceptance/3</li>
<li>validate_change/3 &amp; /4</li>
<li>validate_confirmation/3</li>
<li>validate_exclusion/4 &amp; validate_inclusion/4</li>
<li>validate_format/4</li>
<li>validate_number/3</li>
<li>validate_subset/4</li>
</ul>
<p>&#x5B8C;&#x6574;&#x7684; validators &#x5217;&#x8868;&#x4EE5;&#x53CA;&#x5B83;&#x4EEC;&#x7684;&#x8BE6;&#x7EC6;&#x7528;&#x6CD5;&#x60A8;&#x53EF;&#x4EE5; <a href="https://hexdocs.pm/ecto/Ecto.Changeset.html#summary" target="_blank">&#x5728;&#x8FD9;&#x513F;</a> &#x627E;&#x5230;</p>
<h3 id="&#x81EA;&#x5B9A;&#x4E49;&#x6821;&#x9A8C;">&#x81EA;&#x5B9A;&#x4E49;&#x6821;&#x9A8C;</h3>
<p>&#x5C3D;&#x7BA1;&#x5185;&#x7F6E;&#x6821;&#x9A8C;&#x51FD;&#x6570;&#x6DB5;&#x76D6;&#x4E86;&#x5E7F;&#x6CDB;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x4F46;&#x60A8;&#x53EF;&#x80FD;&#x4ECD;&#x7136;&#x9700;&#x8981;&#x4E0D;&#x540C;&#x7684;&#x6821;&#x9A8C;&#x3002;</p>
<p>&#x5230;&#x76EE;&#x524D;&#x4E3A;&#x6B62;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x7684;&#x6BCF;&#x4E2A; <code>validate_</code> &#x51FD;&#x6570;&#x90FD;&#x63A5;&#x6536;&#x5E76;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; <code>&#xFF05;Ecto.Changeset{}</code> &#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8F7B;&#x677E;&#x5730;&#x63D2;&#x5165;&#x81EA;&#x5DF1;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<p>&#x6BD4;&#x5982;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x786E;&#x4FDD;&#x53EA;&#x5141;&#x8BB8;&#x865A;&#x6784;&#x7684;&#x89D2;&#x8272;&#x540D;&#x79F0;&#x80FD;&#x901A;&#x8FC7;&#x68C0;&#x67E5;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-variable">@fictional_names</span> [<span class="hljs-string">&quot;Black Panther&quot;</span>, <span class="hljs-string">&quot;Wonder Woman&quot;</span>, <span class="hljs-string">&quot;Spiderman&quot;</span>]
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">validate_fictional_name</span></span>(changeset) <span class="hljs-keyword">do</span>
  name = get_field(changeset, <span class="hljs-symbol">:name</span>)

  if name <span class="hljs-keyword">in</span> <span class="hljs-variable">@fictional_names</span> <span class="hljs-keyword">do</span>
    changeset
  else
    add_error(changeset, <span class="hljs-symbol">:name</span>, <span class="hljs-string">&quot;is not a superhero&quot;</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x6211;&#x4EEC;&#x5F15;&#x5165;&#x4E86;&#x4E24;&#x4E2A;&#x65B0;&#x7684;&#x8F85;&#x52A9;&#x51FD;&#x6570;&#xFF1A;<a href="https://hexdocs.pm/ecto/Ecto.Changeset.html#get_field/3" target="_blank"><code>get_field/3</code></a> &#x548C; <a href="https://hexdocs.pm/ecto/Ecto.Changeset.html#add_error/4" target="_blank"><code>add_error/4</code></a>&#x3002; &#x5C3D;&#x7BA1;&#x4ED6;&#x4EEC;&#x7684;&#x4F5C;&#x7528;&#x4F60;&#x770B;&#x51FD;&#x6570;&#x540D;&#x5C31;&#x5E94;&#x8BE5;&#x80FD;&#x731C;&#x5230;&#xFF0C;&#x4F46;&#x6211;&#x8FD8;&#x662F;&#x5EFA;&#x8BAE;&#x60A8;&#x770B;&#x4E00;&#x4E0B;&#x6587;&#x6863;&#x3002;</p>
<p>&#x603B;&#x662F;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; <code>&#xFF05;Ecto.Changeset{}</code> &#x662F;&#x4E00;&#x4E2A;&#x597D;&#x4E60;&#x60EF;&#xFF0C;&#x6240;&#x4EE5;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>|&gt;</code> &#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x4EE5;&#x4FBF;&#x4EE5;&#x540E;&#x6DFB;&#x52A0;&#x66F4;&#x591A;&#x6821;&#x9A8C;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">changeset</span></span>(struct, params) <span class="hljs-keyword">do</span>
  struct
  |&gt; cast(params, [<span class="hljs-symbol">:name</span>])
  |&gt; validate_required([<span class="hljs-symbol">:name</span>])
  |&gt; validate_length(<span class="hljs-symbol">:name</span>, <span class="hljs-symbol">min:</span> <span class="hljs-number">2</span>)
  |&gt; validate_fictional_name()
<span class="hljs-keyword">end</span>
</code></pre>
<pre><code class="lang-elixir">iex&gt; User.changeset(%User{}, %{<span class="hljs-string">&quot;name&quot;</span> =&gt; <span class="hljs-string">&quot;Bob&quot;</span>})
%Ecto.Changeset&lt;
  <span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">changes:</span> %{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Bob&quot;</span>},
  <span class="hljs-symbol">errors:</span> [<span class="hljs-symbol">name:</span> {<span class="hljs-string">&quot;is not a superhero&quot;</span>, []}],
  <span class="hljs-symbol">data:</span> <span class="hljs-comment">#User&lt;&gt;,</span>
  <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">false</span>
&gt;
</code></pre>
<p>&#x975E;&#x5E38;&#x68D2;&#xFF01; &#x4F46;&#x662F;&#xFF0C;&#x5B9E;&#x9645;&#x4E0A;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x5FC5;&#x8981;&#x81EA;&#x5DF1;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x529F;&#x80FD; - <code>validate_inclusion/4</code> &#x51FD;&#x6570;&#x5B8C;&#x5168;&#x53EF;&#x4EE5;&#x4EE3;&#x66FF;&#x5B83;; &#x60A8;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5982;&#x4F55;&#x6DFB;&#x52A0;&#x4E00;&#x4E9B;&#x6709;&#x7528;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x9519;&#x8BEF;&#x3002;</p>
<h2 id="&#x4EE5;&#x7F16;&#x7A0B;&#x65B9;&#x5F0F;&#x6DFB;&#x52A0;&#x66F4;&#x6539;">&#x4EE5;&#x7F16;&#x7A0B;&#x65B9;&#x5F0F;&#x6DFB;&#x52A0;&#x66F4;&#x6539;</h2>
<p>&#x6709;&#x65F6;&#x60A8;&#x5E0C;&#x671B;&#x624B;&#x52A8;&#x5BF9; changeset &#x5F15;&#x5165;&#x66F4;&#x6539;&#x3002; <code>put_change/3</code> &#x7684;&#x5B58;&#x5728;&#x5C31;&#x662F;&#x5E2E;&#x52A9;&#x4F60;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x76EE;&#x7684;&#x7684;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x4E0D;&#x8BA9; <code>name</code> &#x5B57;&#x6BB5;&#x6210;&#x4E3A;&#x5FC5;&#x9700;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x5141;&#x8BB8;&#x7528;&#x6237;&#x5728;&#x6CA1;&#x6709;&#x540D;&#x5B57;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x6CE8;&#x518C;&#xFF0C;&#x5E76;&#x79F0;&#x4E4B;&#x4E3A; &quot;&#x533F;&#x540D;&quot;&#x3002; &#x6211;&#x4EEC;&#x9700;&#x8981;&#x7684;&#x51FD;&#x6570;&#x770B;&#x8D77;&#x6765;&#x5F88;&#x719F;&#x6089; - &#x5B83;&#x63A5;&#x53D7;&#x5E76;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; changeset&#xFF0C;&#x5C31;&#x50CF;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x4ECB;&#x7ECD;&#x7684; <code>validate_fictional_name/1</code> &#x4E00;&#x6837;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_name_if_anonymous</span></span>(changeset) <span class="hljs-keyword">do</span>
  name = get_field(changeset, <span class="hljs-symbol">:name</span>)

  if is_nil(name) <span class="hljs-keyword">do</span>
    put_change(changeset, <span class="hljs-symbol">:name</span>, <span class="hljs-string">&quot;Anonymous&quot;</span>)
  else
    changeset
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x53EA;&#x6709;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E2D;&#x6CE8;&#x518C;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x624D;&#x80FD;&#x5C06;&#x7528;&#x6237;&#x540D;&#x8BBE;&#x7F6E;&#x4E3A; <code>&#x533F;&#x540D;</code> ; &#x8981;&#x505A;&#x5230;&#x8FD9;&#x4E00;&#x70B9;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684; changeset &#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">registration_changeset</span></span>(struct, params) <span class="hljs-keyword">do</span>
  struct
  |&gt; cast(params, [<span class="hljs-symbol">:name</span>])
  |&gt; set_name_if_anonymous()
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x4E0D;&#x5FC5;&#x4F20;&#x9012; <code>name</code>&#xFF0C;<code>Anonymous</code> &#x5C06;&#x6309;&#x9884;&#x671F;&#x81EA;&#x52A8;&#x8BBE;&#x7F6E;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; User.registration_changeset(%User{}, %{})
%Ecto.Changeset&lt;
  <span class="hljs-symbol">action:</span> <span class="hljs-keyword">nil</span>,
  <span class="hljs-symbol">changes:</span> %{<span class="hljs-symbol">name:</span> <span class="hljs-string">&quot;Anonymous&quot;</span>},
  <span class="hljs-symbol">errors:</span> [],
  <span class="hljs-symbol">data:</span> <span class="hljs-comment">#User&lt;&gt;,</span>
  <span class="hljs-symbol">valid?:</span> <span class="hljs-keyword">true</span>
&gt;
</code></pre>
<p>&#x5177;&#x6709;&#x7279;&#x5B9A;&#x804C;&#x8D23;&#x7684; changeset &#x521B;&#x5EFA;&#x51FD;&#x6570;&#xFF08;&#x5982; <code>registration_changeset/2</code> &#xFF09;&#x5E76;&#x4E0D;&#x7F55;&#x89C1; - &#x6709;&#x65F6;&#x60A8;&#x9700;&#x8981;&#x7075;&#x6D3B;&#x5730;&#x4EC5;&#x6267;&#x884C;&#x67D0;&#x4E9B;&#x6821;&#x9A8C;&#x6216;&#x8FC7;&#x6EE4;&#x7279;&#x5B9A;&#x53C2;&#x6570;&#x3002; &#x4E0A;&#x9762;&#x7684;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5728;&#x5176;&#x4ED6;&#x5730;&#x65B9;&#xFF0C;&#x6BD4;&#x5982; <code>sign_up/1</code> &#x51FD;&#x6570;&#x4E2D;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">sign_up</span></span>(params) <span class="hljs-keyword">do</span>
  %User{}
  |&gt; User.registration_changeset(params)
  |&gt; Repo.insert()
<span class="hljs-keyword">end</span>
</code></pre>
<h2 id="&#x7ED3;&#x8BED;">&#x7ED3;&#x8BED;</h2>
<p>&#x6211;&#x4EEC;&#x5728;&#x672C;&#x8BFE;&#x4E2D;&#x6CA1;&#x6709;&#x6D89;&#x53CA;&#x5F88;&#x591A;&#x7528;&#x4F8B;&#x548C;&#x529F;&#x80FD;&#xFF0C;&#x4F8B;&#x5982;&#x60A8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7684; <a href="https://hexdocs.pm/ecto/Ecto.Changeset.html#module-schemaless-changesets" target="_blank">schemaless changesets</a> &#x6821;&#x9A8C; <em>&#x4EFB;&#x4F55;</em> &#x6570;&#x636E;; &#x6216;&#x5355;&#x72EC;&#x5904;&#x7406; changeset &#x7684;&#x526F;&#x4F5C;&#x7528;( <a href="https://hexdocs.pm/ecto/Ecto.Changeset.html#prepare_changes/2" target="_blank"><code>prepare_changes/2</code></a> )&#xFF0C;&#x6216;&#x4F7F;&#x7528;&#x5173;&#x8054;&#x548C;&#x5D4C;&#x5165;&#x3002; &#x6211;&#x4EEC;&#x53EF;&#x80FD;&#x4F1A;&#x5728;&#x672A;&#x6765;&#x7684;&#x9AD8;&#x7EA7;&#x8BFE;&#x7A0B;&#x4E2D;&#x4ECB;&#x7ECD;&#x8FD9;&#x4E9B;&#x5185;&#x5BB9;&#xFF0C;&#x4F46;&#x4E0E;&#x6B64;&#x540C;&#x65F6; - &#x6211;&#x4EEC;&#x9F13;&#x52B1;&#x63A2;&#x7D22; <a href="https://hexdocs.pm/ecto/Ecto.Changeset.html" target="_blank">Ecto Changeset&#x7684;&#x6587;&#x6863;</a> &#x4EE5;&#x83B7;&#x53D6;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="basics.html" class="navigation navigation-prev " aria-label="Previous page: Basics">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="associations.html" class="navigation navigation-next " aria-label="Next page: 关联关系">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.1.2","title":"Changesets","level":"3.2","depth":1,"next":{"title":"关联关系","level":"3.3","depth":1,"path":"lessons/ecto/associations.md","ref":"lessons/ecto/associations.md","articles":[]},"previous":{"title":"Basics","level":"3.1","depth":1,"path":"lessons/ecto/basics.md","ref":"lessons/ecto/basics.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/ecto/changesets.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

